﻿namespace CN2P.Tests
{
    using System;
    using System.Collections.Generic;

    using CN2P.ServiceModel;
    using CN2P.ServiceModel.ServiceClients;

    public abstract class BaseServiceTest
    {
        private ECommerceServiceClient service;

        protected ECommerceServiceClient Service
        {
            get
            {
                if (this.service == null)
                {
                    var config = ECommerceServiceClientConfiguration.FromAppConfig();
                    this.service = new ECommerceServiceClient(config);
                }

                return this.service;
            }
        }

        protected static string GetRandomUserId()
        {
            return Guid.NewGuid().GetHashCode().ToString("x").PadLeft(8, '0') + "@hotmail.com";
        }

        protected static FullProductModel GetRandomProduct()
        {
            return new FullProductModel
            {
                ProdName = Guid.NewGuid().ToString(),
                ProdDesc = Guid.NewGuid().ToString(),
                ProdPrice = 1,
                ImageUrl = string.Empty,
                ManufDate = DateTime.Now.ToString("yyyy-MM-dd"),
                Tags = new List<string>()
            };
        }

        protected static ProductReviewModel GetRandomProductReview(string prodId, string userId)
        {
            return new ProductReviewModel
            {
                ProdId = prodId,
                Comments = "aaa",
                Rating = 2,
                ReviewDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"),
                UserId = userId
            };
        }

        protected FullProductModel CreateRandomProduct()
        {
            var prod = GetRandomProduct();
            try
            {
                var prodId = this.Service.ProductsService.CreateProduct(prod);
                prod.ProdId = prodId;
            }
            catch (Exception e)
            {
                throw new Exception("Couldn't create random product", e);
            }
            return prod;
        }

        protected ProductReviewModel CreateRandomProductReview(FullProductModel product, string userId)
        {
            return this.CreateRandomProductReview(product.ProdId, userId);
        }

        protected ProductReviewModel CreateRandomProductReview(string prodId, string userId)
        {
            try
            {
                var review = GetRandomProductReview(prodId, userId);
                var reviewId = this.Service.ProductsService.AddReview(prodId, review);
                review.ReviewId = reviewId;
                return review;
            }
            catch (Exception ex)
            {
                throw new Exception("Couldn't submit product review", ex);
            }
        }

        protected CartModel CreateRandomCart(string userId)
        {
            var prod1 = this.CreateRandomProduct();
            var prod2 = this.CreateRandomProduct();

            var item1 = prod1.CreateCartItem();
            var item2 = prod2.CreateCartItem();
            var cart = new CartModel
            {
                UserId = userId,
                Items = new[] { item1, item2 }
            };
            this.Service.CartService.AddCart(userId, cart);
            return cart;
        }
    }
}
